<template>
  <div class="quickbird-快递100物流配置-set gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('sysManagement.quickAliConfig') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="180px"
      size="default"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        :label="`${$t('sysManagement.quickAliCode')}:`"
        style="width:640px"
        prop="aliCode"
      >
        <el-input
          v-model="dataForm.aliCode"
          :placeholder="`${$t('sysManagement.quickAliCode')}`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.quickAliConfig')} reqUrl:`"
        style="width:640px"
        prop="reqUrl"
      >
        <el-input
          v-model="dataForm.reqUrl"
          :placeholder="`${$t('sysManagement.quickAliConfig')} reqUrl`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.isActive')"
        style="width:640px"
        prop="isOpen"
      >
        <el-switch
          v-model="dataForm.isOpen"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>

      <div
        class="default-btn"
        @click="onSubmit()"
      >
        {{ $t('sysManagement.save') }}
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const emit = defineEmits(['activationConfig'])
const props = defineProps({
  saveUrl: {
    type: String,
    default: '/sys/pconfig/save'
  },
  changeFlag: {
    type: Boolean,
    default: false
  }
})

const dataForm = ref({
  aliCode: null,
  reqUrl: null,
  isOpen: false
})
const dataFormRef = ref(null)
let activationEventOriginate = ref(false)
const dataRule = {
  aliCode: [
    { required: true, message: `code ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  reqUrl: [
    { required: true, message: `Url ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ]
}
watch(() => props.changeFlag, () => {
  if (!activationEventOriginate.value) {
    dataForm.value.isOpen = false
  }
  activationEventOriginate.value = false
})
onMounted(() => {
  init()
})

const init = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/sys/pconfig/info/' + 'ALI_QUICK_CONFIG'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data) {
        dataForm.value = JSON.parse(data)
      }
    })
    dataFormRef.value?.resetFields()
  })
}
// 表单提交
const onSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl(props.saveUrl),
        method: 'post',
        data: http.adornData({
          paramKey: 'ALI_QUICK_CONFIG',
          paramValue: JSON.stringify(dataForm.value),
          remark: '阿里快递物流配置'
        })
      }).then(({ data }) => {
        if (data) {
          init()
        }
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            if (dataForm.value.isOpen) {
              activationEventOriginate = true
              emit('activationConfig')
            }
          }
        })
      })
    }
  })
}
defineExpose({ init })
</script>
